package com.taotao.rop.service;

import com.rop.annotation.HttpAction;
import com.rop.annotation.NeedInSessionType;
import com.rop.annotation.ServiceMethod;
import com.rop.annotation.ServiceMethodBean;
import com.taotao.rop.database.domain.OtherTel;
import com.taotao.rop.database.domain.Tel;
import com.taotao.rop.database.mapper.OtherTelMapper;
import com.taotao.rop.database.mapper.TelMapper;
import com.taotao.rop.response.BaseResponse;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@ServiceMethodBean(httpAction = HttpAction.POST, version = "1.0")
public class TelService {
    @Autowired
    private TelMapper telMapper;
    @Autowired
    private OtherTelMapper otherTelMapper;

    @ServiceMethod(method = "tel.list", needInSession = NeedInSessionType.NO)
    public Object getList(Tel request) {
        List<Tel> telList = telMapper.getList(request);

        BaseResponse<List<Tel>> response = new BaseResponse<List<Tel>>();
        response.setSuccess(true);
        response.setData(telList);
        return response;
    }

    @ServiceMethod(method = "tel.insert")
    public Object insertTel(Tel request) {
        List<Tel> telList = telMapper.getList(request);
        int count = 0;
        if (telList == null || telList.size() < 1) {
            //插入
            count = telMapper.insertOne(request);
        }

        BaseResponse<Integer> response = new BaseResponse<Integer>();
        response.setSuccess(true);
        response.setData(count);
        if (count == 0) {
            response.setMsg("该号码已经存在");
        }
        return response;
    }

    @ServiceMethod(method = "otherTel.insert", needInSession = NeedInSessionType.NO)
    public Object insertOtherTel(OtherTel request) {
        List<OtherTel> otherTelList = otherTelMapper.getList(request);
        int count = 0;
        if (otherTelList == null || otherTelList.size() < 1) {
            //插入
            count = otherTelMapper.insertOne(request);
        }

        BaseResponse<Integer> response = new BaseResponse<Integer>();
        response.setSuccess(true);
        response.setData(count);
        return response;
    }

    @ServiceMethod(method = "otherTel.delete", needInSession = NeedInSessionType.NO)
    public Object deleteOtherTel(OtherTel request) {
        int count = otherTelMapper.deleteOne(request.getNoSegment());

        BaseResponse<Integer> response = new BaseResponse<Integer>();
        response.setSuccess(true);
        response.setData(count);
        return response;
    }

    @ServiceMethod(method = "otherTel.listByPage")
    public Object getListByPage(OtherTel request) {
        List<OtherTel> otherTelList = otherTelMapper.getListByPage(request);

        BaseResponse<List<OtherTel>> response = new BaseResponse<List<OtherTel>>();
        response.setSuccess(true);
        response.setMsg("" + otherTelMapper.getCount(request));
        response.setData(otherTelList);
        return response;
    }
}
